﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmUnitBase_Editor : XtraForm
    {
        public FrmUnitBase_Editor()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 主键值，非正零为新增
        /// </summary>
        public int UnitBaseID;
        /// <summary>
        /// 标题栏文字格式
        /// </summary>
        private string captionFormat;

        private void FrmUnitBase_Editor_Load(object sender, EventArgs e)
        {
            captionFormat = Text;
        }
        private void btnFirst_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.UnitBase();
            var row = bll.GetFirst();
            if (row != null)
            {
                UnitBaseID = Convert.ToInt32(row["UnitBaseID"]);
                LoadData(UnitBaseID);
            }
            else
            {
                NewRecord();
            }
        }
        private void btnPrevious_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (UnitBaseID <= 0)
            {
                return;
            }
            var bll = new DMZS.BLL.UnitBase();
            var row = bll.GetPrevious(UnitBaseID);
            if (row != null)
            {
                UnitBaseID = Convert.ToInt32(row["UnitBaseID"]);
                LoadData(UnitBaseID);
            }
            else
            {
                LoadData(UnitBaseID);
            }
        }
        private void btnNext_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (UnitBaseID <= 0)
            {
                return;
            }
            var bll = new DMZS.BLL.UnitBase();
            var row = bll.GetNext(UnitBaseID);
            if (row != null)
            {
                UnitBaseID = Convert.ToInt32(row["UnitBaseID"]);
                LoadData(UnitBaseID);
            }
            else
            {
                LoadData(UnitBaseID);
            }
        }
        private void btnLast_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var bll = new DMZS.BLL.UnitBase();
            var row = bll.GetLast();
            if (row != null)
            {
                UnitBaseID = Convert.ToInt32(row["UnitBaseID"]);
                LoadData(UnitBaseID);
            }
            else
            {
                NewRecord();
            }
        }
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (UnitBaseID <= 0)
            {
                return;
            }
            NewRecord();
        }
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ValidateInput())
            {
                return;
            }
            if (UnitBaseID <= 0)
            {
                SavaNew();
            }
            else
            {
                SaveModify();
            }
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        private void SaveModify()
        {
            var model = new DMZS.Model.UnitBase();
            var bll = new DMZS.BLL.UnitBase();

            model.UnitBaseID = UnitBaseID;
            model.BaseCode = txtBaseCode.Text.Trim() ;
            model.BaseName = txtBaseName.Text.Trim();
            model.IsStop = chkIsStop.Checked ? 1 : 0;
            model.Deleted = false;

            var result = bll.UpdateExpand(model);
            if (result == -1001)
            {
                XtraMessageBox.Show("单位编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtBaseCode.SelectAll();
                txtBaseCode.Focus();
            }
            else
            {
                if (result != 0)
                {
                    XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    UnitBaseID = result;
                    LoadData(UnitBaseID);
                }
                else
                {
                    XtraMessageBox.Show("添加失败，建议重新打开界面。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
            }
        }
        /// <summary>
        /// 保存新记录
        /// </summary>
        private void SavaNew()
        {
            var model = new DMZS.Model.UnitBase();
            var bll = new DMZS.BLL.UnitBase();

            model.BaseCode = txtBaseCode.Text.Trim();
            model.BaseName = txtBaseName.Text.Trim();
            model.IsStop = chkIsStop.Checked ? 1 : 0;
            model.Deleted = false;

            var result = bll.AddExpand(model);
            if (result == -1001)
            {
                XtraMessageBox.Show("单位编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtBaseCode.SelectAll();
                txtBaseCode.Focus();
            }
            else
            {
                if (result != 0)
                {
                    XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    UnitBaseID = result;
                    LoadData(UnitBaseID);
                }
                else
                {
                    XtraMessageBox.Show("添加失败，建议重新打开界面。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
            }
        }
        /// <summary>
        /// 验证输入
        /// </summary>
        /// <returns></returns>
        private bool ValidateInput()
        {
            if (txtBaseCode.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show("请输入单位编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtBaseCode.Focus();
                return false;
            }
            else
            {
                if (txtBaseName.Text.Trim().Length == 0)
                {
                    XtraMessageBox.Show("请输入单位名称。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    txtBaseName.Focus();
                    return false;
                }
            }
            return true;
        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (UnitBaseID <= 0)
            {
                NewRecord();
                return;
            }
            else
            {
                if (DialogResult.No == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    return;
                }
            }
            var bll = new DMZS.BLL.UnitBase();
            bll.DeleteExpand(UnitBaseID);

            NewRecord();
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (UnitBaseID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData(UnitBaseID);
            }
        }

        private void FrmUnitBase_Editor_Shown(object sender, EventArgs e)
        {
            if (UnitBaseID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData(UnitBaseID);
            }
        }
        /// <summary>
        /// 记载数据
        /// </summary>
        /// <param name="UnitBaseID"></param>
        private void LoadData(int UnitBaseID)
        {
            var bll = new DMZS.BLL.UnitBase();
            var model = bll.GetModel(UnitBaseID);

            if (model == null)
            {
                XtraMessageBox.Show("该条数据已经被删除，请刷新数据", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                NewRecord();
                return;
            }

            Text = string.Format(captionFormat, "维护");
            txtBaseCode.Text = model.BaseCode;
            txtBaseName.Text = model.BaseName;
            chkIsStop.Checked = Convert.ToBoolean(model.IsStop);

            txtBaseCode.Properties.ReadOnly = true;
            txtBaseCode.TabStop = false;
            txtBaseName.Focus();
        }
        /// <summary>
        /// 新记录
        /// </summary>
        private void NewRecord()
        {
            Text = string.Format(captionFormat, "新增");
            UnitBaseID = 0;
            txtBaseCode.Text = string.Empty;
            txtBaseName.Text = string.Empty;
            chkIsStop.Checked = false;

            txtBaseCode.Properties.ReadOnly = false;
            txtBaseCode.TabStop = true;
            txtBaseCode.Focus();
        }
    }
}
